<!DOCTYPE html>
<html>
<head>
    <head/>
    <div th:include="base-libs-easyui :: base-libs-easyui"/>
    <title>医疗机构科室信息</title>
    <style type="text/css">
        body {
            padding: 0px 10px;
        }
    </style>
</head>
<script th:inline="javascript">
    /**读取request中的初始化数据*/
    var requestData = [[${requestData}]];
</script>
<script type="text/javascript">
    $(function () {
        initView();
        initData();
        queryBzksxx();
    });

    /**查询标准科室值域*/
    function queryBzksxx(){
        var uri = zydmService + "/ksxx/queryAll";
        $.QuickRemote.AjaxJson(uri, queryBzksxxHandler);
    }
    function queryBzksxxHandler(result){
        $("body").data("bzks", result);
        $("#btnQuery").trigger("click");
    }

    function initData() {
        if (!requestData["isManager"]) {
            var PTJGID = requestData["PTJGID"];
            var YLJGMC = requestData["YLJGMC"];
            var value = {PTJGID: PTJGID, YLJGMC: YLJGMC};
            $("#combogridYljg").combogrid("grid").datagrid("loadData", [value]);
            $("#combogridYljg").combogrid("setValue", value);
        }
    }

    function queryForm() {
        clearDgSecondKsxx();
        $("#dgForm").datagrid({url: contextRoot + "/ksxx/queryPageKsxx.service"});
    }

    function initView() {
        $("#dgForm").datagrid({
            idField: "KSID",
            striped: true,
            fitColumns: true,
            fit: true,
            border: false,
            rownumbers: true,
            singleSelect: true,
            pagination: true,
            loadMsg: "",
            title: "一级科室",
            columns: [[
                {
                    field: "YLJGMC",
                    title: "医疗机构名称"
                    ,width: 7
                },
                {
                    field: "KSMC",
                    title: "科室名称"
                    ,width: 6
                },
                {
                    field: "PTKSDM",
                    title: "标准科室名称"
                    ,formatter: function (value, row, index) {
                        var text = "";
                        if(!StringUtils.isEmpty(value)){
                            var bzksAry = $("body").data("bzks");
                            $.each(bzksAry, function(index, item){
                                var xmdm = item["XMDM"];
                                if(xmdm == value){
                                    text = item["XMMC"];
                                    return false;
                                }
                            })
                        }
                        return text;
                    }
                    ,width: 6
                },
                {
                    field: "SFTSKS",
                    title: "特色科室"
                    ,formatter: function (value, row, index) {
                        if("0" == value){
                            return "否";
                        }else{
                            return "是";
                        }
                    }
                    ,width: 3
                }
                ,{
                    field: "CZ",
                    title: "操作"
                    ,width: 6
                    ,formatter: function (value, row, index) {
                        var tableToolBar = $("#tableToolBar").html();
                        var KSID = row["PTJGID"] + "|" + row["KSDM"] + "|" + row["JB"];
                        row["KSID"] = KSID;
                        return tableToolBar.replace(/KSID/g, KSID);
                    }
                }
            ]]
            ,onBeforeLoad: function (queryParams) {
                var url = $(this).datagrid("options")["url"];
                /*URL不等空时再发送请求*/
                if (!StringUtils.isEmpty(url)) {
                    queryParams["pageSize"] = queryParams["rows"];
                    queryParams["pageNumber"] = queryParams["page"];
                    /*获取查询条件*/
                    var formParams = getQueryFormValue();
                    queryParams = $.extend(queryParams, formParams);
                    return true;
                } else {
                    return false;
                }
            }
            ,onDblClickRow: function(index,row){
                clearDgSecondKsxx();
                querySecondJgKsxx();
            }
        });
        $("#dgForm").datagrid("resize");

        $("#dgSecondKsxx").datagrid({
            idField: "KSID",
            striped: true,
            fitColumns: true,
            fit: true,
            border: false,
            rownumbers: true,
            singleSelect: true,
            loadMsg: "",
            title: "二级级科室",
            columns: [[
                {
                    field: "KSDM",
                    title: "科室代码"
                    ,width: 8
                },
                {
                    field: "KSMC",
                    title: "科室名称"
                    ,width: 6
                },
                {
                    field: "CZ",
                    title: "操作"
                    ,width: 4
                    ,formatter: function (value, row, index) {
                        var tableSecondToolBar = $("#tableSecondToolBar").html();
                        var KSID = row["PTJGID"] + "|" + row["KSDM"] + "|" + row["JB"];
                        row["KSID"] = KSID;
                        return tableSecondToolBar.replace(/KSID/g, KSID);
                    }
                }
            ]]
        });
        $("#dgSecondKsxx").datagrid("resize");

        $("body").on("click", ".tableBtn", function () {
            var action = $(this).attr("action");
            var KSID = $(this).attr("id");
            $("#dgForm").datagrid("selectRecord", KSID);
            var item = $("#dgForm").datagrid("getSelected");
            if ("update" == action) {
                updateKsxx(item);
            } else if ("delete" == action) {
                deleteKsxx(item);
            } else if ("addSecond" == action) {
                addSecond(item)
            }
        })

        $("body").on("click", ".tableSecond", function(){
            var action = $(this).attr("action");
            var KSID = $(this).attr("id");
            $("#dgSecondKsxx").datagrid("selectRecord", KSID);
            /*选择的二级科室*/
            var item = $("#dgSecondKsxx").datagrid("getSelected");
            if ("update" == action) {
                /*选择的父级科室*/
                var SJKSXX = $("#dgForm").datagrid("getSelected");
                updateSecondKsxx(SJKSXX, item);
            } else if ("delete" == action) {
                deleteSecondKsxx(item);
            }
        })

        var readonly = !requestData["isManager"];
        $("#combogridYljg").combogrid({
            height: 36
            , width: 300
            , panelWidth: 500
            , singleSelect: true
            , pagination: true
            , idField: 'PTJGID'
            , textField: 'YLJGMC'
            , mode: "remote"
            , readonly: readonly
            , hasDownArrow: !readonly
            , url: contextRoot + "/yljgxx/queryPageSummary.service"
            , delay: 1000
            , reversed: false
            , columns: [[
                {
                    field: 'YLJGMC'
                    , title: '机构名称'
                    , width: 240
                }
                , {
                    field: 'XZQHMC'
                    , title: '行政区划'
                    , width: 80
                }
                , {
                    field: 'WSJGLBMC'
                    , title: '机构类别'
                    , width: 80
                }
                , {
                    field: 'YYDJMC'
                    , title: '机构等级'
                    , width: 80
                }
            ]]
            , onBeforeLoad: function (queryParams) {
                var url = $(this).datagrid("options")["url"];
                /*URL不等空时再发送请求*/
                if (!StringUtils.isEmpty(url)) {
                    queryParams["pageSize"] = queryParams["rows"];
                    queryParams["pageNumber"] = queryParams["page"];
                    queryParams["YLJGMC"] = queryParams["q"];
                    return true;
                } else {
                    return false;
                }
            }
        });

        if(readonly){
            //隐藏机构名称的查询条件
            $(".hidden").css('display',"none");
        }
    }

    /**获取查询表单的值*/
    function getQueryFormValue() {
        var params = {};
        var item = $("#combogridYljg").combogrid("grid").datagrid("getSelected");
        if(item){
            params["PTJGID"] = item["PTJGID"];
        }else{
            params["YLJGMC"] = $("#combogridYljg").combogrid("getText");
        }
        params["KSMC"] = $("#KSMC").val();
        return params;
    }

    /**修改一级科室*/
    function updateKsxx(item) {
        var updatedItem = $.extend({}, item);
        StoreCache.setCache("curr_jgksxx", updatedItem);
        openManager();
    }
    /**删除一级科室*/
    function deleteKsxx(item){
        var tip = "您确定要删除该科室信息吗？"
        if(!StringUtils.isEmpty(item["KSSL"])){
            tip = "删除该一级科室信息会连同该科室下的"+ item["KSSL"] +"个二级科室一并删除，您确定删除吗?"
        }
        $.QuickAlert.alertConfirm({
            content: tip
            ,callback: function () {
                var url = contextRoot + "/ksxx/deleteJgKsxx";
                $.QuickRemote.AjaxJson(url, function (result) {
                    if (result["state"]) {
                        $.QuickAlert.alertNormal("删除成功");
                        $("#btnQuery").trigger("click");
                    }
                    else {
                        $.QuickAlert.alertNormal("删除失败");
                    }
                }, item)
            }
        })
    }

    /**查询按钮点击回调*/
    function btnQueryHandler() {
        queryForm();
    }

    /**新增一级科室回调*/
    function btnAddHandler() {
        openManager();
    }
    
    /** 导出Excel文件  */
	function btnDownExcelHandler(){
		var formParams = getQueryFormValue();
		var $downloadForm = $("#downloadForm");
		$("#downloadForm").empty();
		$.each(formParams, function(key,val){
			var input = $("<input>").attr({name: key, value: val, type: 'hidden'});
			$downloadForm.append(input);
		})
		$downloadForm.submit();
	}

    /**打开一级科室编辑页面*/
    function openManager(){
        var uri = contextRoot + "/ksxx/manager.html?";
        $.QuickAlert.alertIFrame({
            title: "机构一级科室信息维护"
            ,shadeClose: false
            ,content: uri
            ,width: "660px"
            ,height: "300px"
            ,end: function(){
                /*重载数据*/
                $("#dgForm").datagrid("reload");
            }
        })
    }

    /**添加二级科室*/
    function addSecond(SJKSXX){
        openManagerSecond(SJKSXX);
    }
    /**更新二级科室*/
    function updateSecondKsxx(SJKSXX, item){
        openManagerSecond(SJKSXX, item);
    }
    /**删除二级科室*/
    function deleteSecondKsxx(item){
        $.QuickAlert.alertConfirm({
            content: "确定删除选中的记录吗 ？"
            ,callback: function () {
                var url = contextRoot + "/ksxx/deleteJgKsxx";
                $.QuickRemote.AjaxJson(url, function (result) {
                    if (result["state"]) {
                        $.QuickAlert.alertNormal("删除成功");
                        var index = $("#dgSecondKsxx").datagrid("getRowIndex", item);
                        $("#dgSecondKsxx").datagrid("deleteRow", index);
                    }
                    else {
                        $.QuickAlert.alertNormal("删除失败");
                    }
                }, item)
            }
        })
    }

    /**打开二级科室编辑页面*/
    function openManagerSecond(SJKSXX, item){
        StoreCache.setCache("curr_sjksxx", SJKSXX);
        if(item){
            StoreCache.setCache("curr_jgksxx_second", item);
        }
        var uri = contextRoot + "/ksxx/managerSecond.html?";
        $.QuickAlert.alertIFrame({
            title: "机构二级科室信息维护"
            ,shadeClose: false
            ,content: uri
            ,width: "660px"
            ,height: "300px"
            ,end: function(){
                querySecondJgKsxx();
            }
        })
    }

    /**清空二级科室*/
    function clearDgSecondKsxx(){
        $("#dgSecondKsxx").datagrid("loadData", []);
    }
    /**查询二级科室*/
    function querySecondJgKsxx(){
        var row = $("#dgForm").datagrid("getSelected");
        var PTJGID = row["PTJGID"];
        var KSDM = row["KSDM"];
        var params = {PTJGID: PTJGID, KSDM: KSDM};
        var url = contextRoot + "/ksxx/querySecondJgKsxx";
        $.QuickRemote.AjaxJson(url, function (result) {
            $("#dgSecondKsxx").datagrid("loadData", result);
        }, params);
    }

    /**查询按钮点击回调*/
    function btnQueryHandler() {
        queryForm();
    }

</script>

<body class="easyui-layout">
<div region="north" style="height: 94px; border: none; overflow: hidden; background-color: #f9f9f9;">
    <div style="width: 100%; height: 41px; margin-left: 30px">
        <h3 style="line-height: 41px; margin: 0px; font-size: 18px;font-weight: bold;">医疗机构科室信息</h3>
    </div>
    <div class="datagrid-toolbar" style="padding:8px 20px;">
        <form id="queryForm">
            <table>
                <tr>
                    <td class="query-text hidden">机构名称</td>
                    <td class="query-content hidden">
                        <input id="combogridYljg">
                    </td>
                    <td class="query-text">科室名称</td>
                    <td class="query-content">
                        <input id="KSMC" name="KSMC" class="query-input">
                    </td>
                    <td>
                        <button id="btnQuery" class="btn btn-default" type="button" onclick="btnQueryHandler()">查询</button>
                        <button id="btnAdd" class="btn btn-default" type="button" onclick="btnAddHandler()">新增</button>
                        <button id="btnDown" class="btn btn-default" type="button" onclick="btnDownExcelHandler()">导出</button>
                    </td>
                </tr>
            </table>
        </form>
        
        <form id="downloadForm" action="downloadToExcel.service" enctype="multipart/form-data">
        </form>
    </div>
</div>
<div region="center">
    <div class="easyui-layout" fit="true" style="border: none;">
        <div region="center" style="border: none; border-right: 1px solid #e6e6e6;">
            <table id="dgForm"></table>
        </div>
        <div region="east" style="width: 400px; margin-left: 10px; border: none; border-left: 1px solid #e6e6e6;">
            <table id="dgSecondKsxx"></table>
        </div>
    </div>
</div>
</body>
<script type="text/html" id="tableToolBar">
    <div>
        <a class="tableBtn" action="update" id=KSID href="javascript:void(0)" title="修改"
           style="color: #00c297;">修改</a>
        <a class="tableBtn" action="delete" id=KSID href="javascript:void(0)" title="删除" style="color: #00c297;">删除</a>
        <a class="tableBtn" action="addSecond" id=KSID href="javascript:void(0)" title="添加二级科室"
           style="color: #00c297;">添加二级科室</a>
    </div>
</script>

<script type="text/html" id="tableSecondToolBar">
    <div>
        <a class="tableSecond" action="update" id=KSID href="javascript:void(0)" title="修改" style="color: #00c297;">修改</a>
        <a class="tableSecond" action="delete" id=KSID href="javascript:void(0)" title="删除" style="color: #00c297;">删除</a>
    </div>
</script>
</html>



